<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace App\Admin\Service;

use App\Admin\Mapper\OrderMapper;
use App\Model\Order;
use App\Model\Staff;
use App\Overwrites\AbstractService;
use App\Utils\AliyunDyvmsapi;
use Mine\Annotation\Transaction;

/**
 * 订单列表服务类
 */
class OrderService extends AbstractService
{
    /**
     * @var OrderMapper
     */
    public $mapper;

    public function __construct(OrderMapper $mapper)
    {
        $this->mapper = $mapper;
    }

    /**
     * 分配员工操作
     * @param mixed $id
     * @param array $params
     * @return bool
     */
    #[Transaction]
    public function operateAssignStaff(mixed $id, array $params): bool
    {
        // 查询
        $staff = Staff::query()
            ->where('id', $params['staff_id'])
            ->firstOrFail(['id', 'phone']);
        $order = Order::query()
            ->where('id', $id)
            ->firstOrFail();

        // 更新
        $order->staff_id = $staff->id;
        $order->save();

        // 发送阿里云语音通知
        (new AliyunDyvmsapi())->singleCallByTts($staff->phone, 'TTS_299555415');

        return true;
    }
}
